import { fileURLToPath, URL } from 'node:url'
import basicSsl from '@vitejs/plugin-basic-ssl'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import VueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import externalGlobals from 'rollup-plugin-external-globals'
import { VitePWA } from 'vite-plugin-pwa'

// // https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  console.log(command)
  return {
    base: command === 'serve' ? './' : '//s.thsi.cn/cd/website-f10-pluto/v3.2/',
    plugins: [
      vue(),
      VueDevTools(),
      // basicSsl(),
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      }),
      VitePWA({
        registerType: 'autoUpdate',
        injectRegister: false,
        manifest: {
          name: 'AI-F10',
          short_name: 'F10',
          icons: [
            {
              src: './favicon.ico',
              sizes: '32x32',
              type: 'image/x-icon'
            },
            {
              src: './favicon.ico',
              sizes: '512x512',
              type: 'image/x-icon'
            }
          ],
          start_url: '/',
          display: 'standalone',
          background_color: '#000000',
          theme_color: '#4DBA87'
        },
        injectManifest: {
          injectionPoint: undefined
        },
        strategies: 'injectManifest',
        srcDir: 'public',
        filename: 'sw.js',

        devOptions: {
          // 如果想在`vite dev`命令下调试PWA, 必须启用它
          enabled: true,
          // Vite在dev模式下会使用浏览器原生的ESModule，将type设置为`"module"`与原先的保持一致
          type: 'module'
        }
      })
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    build: {
      outDir: '../resource/v3.2',
      cssCodeSplit: false,
      rollupOptions: {
        external: ['sortablejs', 'muuri'],
        plugins: [
          externalGlobals({
            sortablejs: 'Sortable',
            muuri: 'Muuri'
          })
        ]
      }
    },
    server: {
      open: false,
      host: 'dev.10jqka.com.cn',
      port: 8080,
      https: false,
      proxy: {
        '/basicapi': {
          target: 'http://data.10jqka.com.cn/',
          changeOrigin: true
        },
        '/spider': {
          target: 'http://cbasspider.10jqka.com.cn:8443/',
          changeOrigin: true
        }
      }
    }
  }
})
